package logic

import (
	"context"
	"nove/models/mysql"
	"nove/noverpc/internal/svc"
	"nove/noverpc/noverpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentListLogic {
	return &CommentListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentListLogic) CommentList(in *noverpc.CommentListRequest) (*noverpc.CommentListResponse, error) {
	// todo: add your logic here and delete this line

	var comment []mysql.Comment

	query := mysql.DB.Debug().Model(&mysql.Comment{})

	query.Order("comments.created_at desc").Find(&comment)
	var commentList []*noverpc.CommentList

	for _, bo := range comment {
		list := noverpc.CommentList{
			Content: bo.Content,
			BookId:  int64(bo.BookId),
		}
		commentList = append(commentList, &list)
	}
	return &noverpc.CommentListResponse{
		List: commentList,
	}, nil
}
